package com.example.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.train.business.domain.Daily_TrainCarriage;
import com.example.train.business.domain.Daily_TrainCarriageExample;
import com.example.train.business.domain.Train_Carriage;
import com.example.train.business.enums.SeatColEnum;
import com.example.train.business.mapper.Daily_TrainCarriageMapper;
import com.example.train.business.req.Daily_TrainCarriageQueryReq;
import com.example.train.business.req.Daily_TrainCarriageSaveReq;
import com.example.train.business.resp.Daily_TrainCarriageQueryResp;
import com.example.train.common.resp.PageResp;
import com.example.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Daily_TrainCarriageService {
    private static final Logger LOG = LoggerFactory.getLogger(Daily_TrainCarriageService.class);

    @Resource
    private Daily_TrainCarriageMapper daily_TrainCarriageMapper;

    @Resource
    private Train_CarriageService train_CarriageService;

    /**
     * 乘客注册请求的服务类
     * @param req
     */
    public void save(Daily_TrainCarriageSaveReq req) {
        DateTime now = DateTime.now();

        // 自动计算出列数和总座位数
        List<SeatColEnum> seatColEnums = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(seatColEnums.size());
        req.setSeatCount(req.getColCount() * req.getRowCount());

        Daily_TrainCarriage daily_TrainCarriage = BeanUtil.copyProperties(req, Daily_TrainCarriage.class);
        if(ObjectUtil.isNull(daily_TrainCarriage.getId())){
            //在service中对以下三种数据赋值，因为请求参数不带这三个值，需要服务端自行设置
            daily_TrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            daily_TrainCarriage.setCreateTime(now);
            daily_TrainCarriage.setUpdateTime(now);
            daily_TrainCarriageMapper.insert(daily_TrainCarriage);
        }else{
            daily_TrainCarriage.setUpdateTime(now);
            daily_TrainCarriageMapper.updateByPrimaryKey(daily_TrainCarriage);
        }
    }
    public PageResp<Daily_TrainCarriageQueryResp> queryList(Daily_TrainCarriageQueryReq req) {
        Daily_TrainCarriageExample daily_TrainCarriageExample = new Daily_TrainCarriageExample();
        daily_TrainCarriageExample.setOrderByClause("date asc,train_code asc,date desc");
        Daily_TrainCarriageExample.Criteria criteria = daily_TrainCarriageExample.createCriteria();
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        if(ObjectUtil.isNotNull(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }

        LOG.info("查询页码是 {}", req.getPage());
        LOG.info("每页条数是 {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Daily_TrainCarriage> daily_TrainCarriageList = daily_TrainCarriageMapper.selectByExample(daily_TrainCarriageExample);

        PageInfo<Daily_TrainCarriage> pageInfo = new PageInfo<>(daily_TrainCarriageList);
        LOG.info("总行数是 {}", pageInfo.getTotal());
        LOG.info("总页数是 {}", pageInfo.getPages());

        List<Daily_TrainCarriageQueryResp> list = BeanUtil.copyToList(daily_TrainCarriageList, Daily_TrainCarriageQueryResp.class);

        PageResp<Daily_TrainCarriageQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 删除乘客信息的服务实现
     * @param id
     */
    public void delete(Long id) {
        daily_TrainCarriageMapper.deleteByPrimaryKey(id);
    }

    /**
     * 自动生成十五天后的每日车厢信息
     * @param date
     * @param trainCode
     */
    public void genDaily(Date date, String trainCode) {
        LOG.info("生成日期【{}】车次【{}】的车厢信息开始", DateUtil.formatDate(date), trainCode);

        // 删除某日某车次的车厢信息
        Daily_TrainCarriageExample daily_TrainCarriageExample = new Daily_TrainCarriageExample();
        daily_TrainCarriageExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);
        daily_TrainCarriageMapper.deleteByExample(daily_TrainCarriageExample);

        // 查出某车次的所有的车厢信息
        List<Train_Carriage> carriageList = train_CarriageService.selectByTrainCode(trainCode);
        if (CollUtil.isEmpty(carriageList)) {
            LOG.info("该车次没有车厢基础数据，生成该车次的车厢信息结束");
            return;
        }

        for (Train_Carriage trainCarriage : carriageList) {
            DateTime now = DateTime.now();
            Daily_TrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(trainCarriage, Daily_TrainCarriage.class);
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriage.setCreateTime(now);
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriage.setDate(date);
            daily_TrainCarriageMapper.insert(dailyTrainCarriage);
        }
        LOG.info("生成日期【{}】车次【{}】的车厢信息结束", DateUtil.formatDate(date), trainCode);
    }

    /**
     * 根据座位类型选择信息
     * @param date
     * @param trainCode
     * @param seatType
     * @return
     */
    public List<Daily_TrainCarriage> selectBySeatType (Date date, String trainCode, String seatType) {
        Daily_TrainCarriageExample example = new Daily_TrainCarriageExample();
        example.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
        return daily_TrainCarriageMapper.selectByExample(example);
    }

}
